package com.semp.gu.ui.elements;

import java.util.Vector;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import com.semp.gu.domain.remote.RemoteAlbum;
import com.semp.gu.domain.remote.RemoteGallery;

public class GalleryTreeModel implements TreeModel {
	private Vector<TreeModelListener>	treeModelListeners	= new Vector<TreeModelListener>();
	private RemoteGallery				gallery;

	public GalleryTreeModel(RemoteGallery gallery) {
		this.gallery = gallery;
	}

	public Object getChild(Object parent, int index) {
		if (parent instanceof RemoteGallery) {
			return ((RemoteGallery) parent).getAlbums().get(index);
		} else if (parent instanceof RemoteAlbum) {
			return ((RemoteAlbum) parent).getSubAlbums().get(index);
		}
		return null;
	}

	public int getChildCount(Object parent) {
		if (parent instanceof RemoteGallery) {
			return ((RemoteGallery) parent).getAlbums().size();
		} else if (parent instanceof RemoteAlbum) {
			return ((RemoteAlbum) parent).getSubAlbums().size();
		}
		return 0;
	}

	public int getIndexOfChild(Object parent, Object child) {
		if (parent instanceof RemoteGallery) {
			return ((RemoteGallery) parent).getAlbums().indexOf(child);
		} else if (parent instanceof RemoteAlbum) {
			return ((RemoteAlbum) parent).getSubAlbums().indexOf(child);
		}
		return 0;
	}

	public Object getRoot() {
		return gallery;
	}

	public boolean isLeaf(Object node) {
		if (node instanceof RemoteGallery) {
			return false;
		} else if (node instanceof RemoteAlbum) {
			return ((RemoteAlbum) node).getSubAlbums().size() == 0;
		}
		return true;
	}

	public void removeTreeModelListener(TreeModelListener l) {
		treeModelListeners.remove(l);
	}

	public void addTreeModelListener(TreeModelListener l) {
		treeModelListeners.add(l);
	}

	public void valueForPathChanged(TreePath path, Object newValue) {
		System.out.println("TODO valueForPathChanged");
	}
}
